package com.qiangzhi.controller.admin;

import com.qiangzhi.pojo.dto.CollegeDTO;
import com.qiangzhi.pojo.dto.CollegePageQueryDTO;
import com.qiangzhi.result.Result;
import com.qiangzhi.service.CollegeService;
import com.qiangzhi.pojo.vo.CollegeVO;
import com.qiangzhi.result.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 导入学院和班级相关接口
 */
@RestController
@RequestMapping("/admin/college")
@Api(tags = "导入学院和班级相关接口")
@Slf4j
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    /**
     * 新增学院和班级
     *
     * @param collegeDTO
     * @return
     */
    @PostMapping("/insertCollegeOrMajor")
    @ApiOperation("新增学院和班级")
    @Caching(evict = { // 有新增操作时删除缓存的数据
            @CacheEvict(cacheNames = "allCollegeCache", condition = "#collegeDTO.majorId == null", allEntries = true),
            @CacheEvict(cacheNames = "majorCache", condition = "#collegeDTO.majorId != null", allEntries = true)})
    public Result insertCollegeOrMajor(@RequestBody CollegeDTO collegeDTO) {
        log.info("新增信息：{}", collegeDTO);
        collegeService.insertCollegeOrMajor(collegeDTO);
        return Result.success();
    }

    /**
     * 根据id编辑学院和班级信息
     *
     * @param collegeDTO
     * @return
     */
    @PutMapping({"/updateMajor", "/updateCollege"})
    @ApiOperation("编辑学院和班级信息")
    @Caching(evict = { // 有编辑操作时删除缓存的数据
            @CacheEvict(cacheNames = "allCollegeCache", condition = "#collegeDTO.majorId == null", allEntries = true),
            @CacheEvict(cacheNames = "majorCache", condition = "#collegeDTO.majorId != null", allEntries = true)})
    public Result updateCollegeOrMajor(@RequestBody CollegeDTO collegeDTO) {
        log.info("编辑信息：{}", collegeDTO);
        collegeService.updateCollegeOrMajor(collegeDTO);
        return Result.success();
    }

    /**
     * 学院和班级分页查询
     *
     * @param collegePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("学院和班级分页查询")
    public Result<PageResult> page(CollegePageQueryDTO collegePageQueryDTO) {
        log.info("分页查询：{}", collegePageQueryDTO);
        PageResult pageResult = collegeService.page(collegePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询全部学院
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询全部学院")
    @Cacheable(cacheNames = "allCollegeCache") // 缓存所有学院数据
    public Result<List<CollegeVO>> getAllCollege() {
        log.info("查询全部学院");
        List<CollegeVO> allCollege = collegeService.getAllCollege();
        return Result.success(allCollege);
    }

    /**
     * 根据学院id查询班级
     *
     * @param collegeId
     * @return
     */
    @GetMapping("/{collegeId}")
    @ApiOperation("根据学院id查询班级")
    @Cacheable(cacheNames = "majorCache", key = "#collegeId") // 缓存根据学院id查询到的班级数据
    public Result<List<CollegeVO>> getMajorByCollegeId(@PathVariable Integer collegeId) {
        log.info("根据学院id查询班级：{}", collegeId);
        List<CollegeVO> majorList = collegeService.getMajorByCollegeId(collegeId);
        return Result.success(majorList);
    }

    /**
     * 根据id查询学院或班级详细信息
     *
     * @param collegeDTO
     * @return
     */
    @GetMapping("/major")
    @ApiOperation("根据id查询学院或班级详细信息")
    public Result<CollegeVO> getDetail(CollegeDTO collegeDTO) {
        log.info("查询信息：{}", collegeDTO);
        CollegeVO collegeVO = collegeService.getDetail(collegeDTO);
        return Result.success(collegeVO);
    }

    /**
     * 根据id删除学院信息
     *
     * @param collegeId
     * @return
     */
    @DeleteMapping("/deleteCollege")
    @ApiOperation("根据id删除学院信息")
    @CacheEvict(cacheNames = "allCollegeCache", allEntries = true) // 删除所有学院数据
    public Result deleteByCollegeId(@RequestParam Integer collegeId) {
        log.info("删除学院：{}", collegeId);
        collegeService.deleteByCollegeId(collegeId);
        return Result.success();
    }

    /**
     * 根据id删除班级信息
     *
     * @param majorId
     * @return
     */
    @DeleteMapping("/deleteMajor")
    @ApiOperation("根据id删除班级信息")
    @CacheEvict(cacheNames = "majorCache", allEntries = true) // 删除所有班级数据
    public Result deleteByMajorId(@RequestParam Integer majorId) {
        log.info("删除班级：{}", majorId);
        collegeService.deleteByMajorId(majorId);
        return Result.success();
    }

}
